package com.test.ds.sort.objectsort;

public class ArrayInOb {
	private Person[] a;
	private int nElems;
	
	public ArrayInOb(int max){
		a = new Person[max];
		nElems = 0;
	}
	//----------------------------------------------
	public Person find(String searchName){
		int j;
		for(j=0; j<nElems; j++){
			if( a[j].getLast().equals(searchName)){
				break;
			}
		}
		if(j == nElems){
			return null;
		}else{
			return a[j];
		}
	}
	//-----------------------------------------------
	public void insert(String last, String first, int age){
		a[nElems] = new Person(last, first, age);
		nElems++;
	}
	//-----------------------------------------------
	public boolean delete(String searchName){
		int j;
		for(j=0; j<nElems; j++){
			if(a[j].getLast().equals(searchName)){
				break;
			}
		}
		if(j == nElems){
			return false;
		}else{
			for(int k=j; k<nElems; k++){
				a[k] = a[k+1];
			}
			nElems--;
			return true;
		}
	}
	//------------------------------------------------
	public void display(){
		for(int j =0; j<nElems; j++){
			a[j].displayPerson();
		}
		System.out.println("");
	}
	//------------------------------------------------
	public void insertionSort(){
		int in, out;
		
		for(out=1; out<nElems; out++){
			Person temp = a[out];
			in = out;
			while(in>0 && a[in-1].getLast().compareTo(temp.getLast())>= 0){
				a[in] = a[in-1];
				--in;
			}
			a[in] = temp;
		}
	}
}
